package com.example.jn_dxs.Base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.jn_dxs.R;
import com.example.jn_dxs.Utlis.LoadingUtil;
import com.gyf.immersionbar.ImmersionBar;

import java.lang.reflect.ParameterizedType;
import java.util.Objects;

public abstract class BaseActivity<M extends BaseViewModel, B extends ViewDataBinding> extends AppCompatActivity {

    protected Activity activity;
    protected String TAG;
    protected M model;
    protected B binding;
    protected int requestTag = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        TAG = getClass().getSimpleName();
        binding = DataBindingUtil.setContentView(this, getLayoutId());
        model = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get((Class<M>) Objects.requireNonNull((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
        model.msg.observe(this, this::onMsgChange);
        model.loadingStatus.observe(this, status -> LoadingUtil.setStatus(this, status));
        model.dataArray.observe(this, this::onArrayDataChange);
        model.dataObject.observe(this, this::onObjectDataChange);
        initEventAndView();
        statusBar();
        initClick();
    }

    protected void onArrayDataChange(JSONArray array) {
        LoadingUtil.hide();
    }

    protected void onMsgChange(String msg) {
        LoadingUtil.hide();
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
    }

    protected abstract int getLayoutId();

    protected void onObjectDataChange(JSONObject object) {
        LoadingUtil.hide();
    }

    protected abstract void initEventAndView();

    protected void statusBar() {
        ImmersionBar
                .with(this)
                .reset()
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .keyboardEnable(true)
//                .navigationBarColor(R.color.hl_white)
//                .navigationBarDarkIcon(true)
                .init();
    }

    /**
     * 设置点击事件
     */
    public abstract void initClick();

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }


    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }






}
